<template>
  <!--  委外-->
  <div class="content">
    <div class="pageSearchView">
      <el-form :inline="true" :model="searchParam" class="demo-form-inline">
        <el-form-item label="物料" prop="materialId">
          <el-select v-model="searchParam.materialId" placeholder="请选择物料" filterable  style="width: 200px">
            <el-option v-for="(item, index) of materialList" :key="index" :label="item.materialName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="入库仓库">
          <el-tree-select v-model="searchParam.wareId" :data="wareList" check-strictly clearable filterable
            :render-after-expand="false" :props="{
              children: 'sonList',
              label: 'warehouseName',
              value: 'id',
            }" style="width: 200px" @change="wareChange" />
        </el-form-item>
        <el-form-item label="入库库位">
          <el-select v-model="searchParam.targetStorageId" placeholder="请选择" multiple max-collapse-tags="1" collapse-tags clearable filterable style="width: 250px">
            <el-option v-for="item in storageList" :key="item.id" :label="item.storageName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
          <el-button class="refreshBtn" :icon="Refresh" @click="resetSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="pageTableView">
      <div class="pageTableMenuView">
        <div class="pageTableTitle">列表信息</div>
      </div>
      <!-- 表格 -->
      <el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 320px)">
        <el-table-column label="序号" align="center" width="70" fixed>
          <template #default="scope">
            <!-- 下标 + 1，计算公式：当前页数 * 每页大小 + 当前行的索引 + 1 --> {{ (pageSearch.current - 1) * pageSearch.pageSize +
              scope.$index + 1 }} </template>
        </el-table-column>
        <el-table-column prop="materialNo" label="物料编码" align="center" min-width="250" fixed show-overflow-tooltip />
        <el-table-column prop="materialName" label="物料名称" align="center" min-width="250" show-overflow-tooltip />
        <!-- <el-table-column prop="version" label="物料版本" align="center" min-width="200" /> -->
        <el-table-column prop="batchNo" label="物料二维码" align="center" min-width="200" />
        <el-table-column prop="traceCode" label="追溯码" align="center" min-width="250" />
        <el-table-column prop="materialCount" label="数量" align="center" min-width="200" />
        <el-table-column prop="basicUnit" label="单位" align="center" min-width="200" />
<!--        <el-table-column prop="materialWeigh" label="次单位数量" align="center" min-width="200" />-->
        <el-table-column prop="supplierName" label="供应商" align="center" min-width="200" show-overflow-tooltip />
        <!-- <el-table-column prop="quality" label="有效期(天)" align="center" min-width="200" /> -->
        <!-- <el-table-column prop="quality" label="入库订单号" align="center" min-width="200" /> -->
        <!-- <el-table-column prop="quality" label="入库仓库" align="center" min-width="200" /> -->
        <el-table-column prop="targetStorageName" label="入库库区" align="center" min-width="200" />
        <!-- <el-table-column label="物料状态" prop="status" align="center" min-width="100" :formatter="formatStatus" /> -->
        <!-- <el-table-column prop="batch" label="入库批次" align="center" min-width="200" /> -->
        <el-table-column prop="createTime" label="创建时间" align="center" min-width="200" />
        <el-table-column prop="createName" label="创建人" align="center" min-width="200" />
      </el-table>
      <div class="pagePagination">
        <el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
          :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next" :total="pageTotal"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue';

onMounted(() => {
  getList();
  getOption();
});


const pageSearch = ref({
  current: 1,
  pageSize: 10
});

const storageList = ref({})
const workOrderList = ref({});
const formatStatus = (row, column, cellValue) => {
  switch (cellValue) {
    case 0:
      return '不可用';
    case 1:
      return '可用';
    case 2:
      return '运输中';
    case 3:
      return '生产锁定';
    case 4:
      return '不合格';
    case 5:
      return '报废';
  }
}

const pageTotal = ref(null);//页面总数
const searchParam = ref({});//页面检索条件



// 判断质检类型
const getQualityTypeByValue = (e) => {
  let name = null;
  for (const item of qualityTypeList) {
    if (e == item.value) {
      name = item.label;
    }
  }
  return name;
};

// 质检类型
const qualityTypeList = reactive([
  {
    label: '部分送货',
    value: '1',
  },
  {
    label: '全部送货',
    value: '2',
  },
  {
    label: '未送货',
    value: '0',
  }
  // 继续添加更多检查类型...
]);


const paginatedData = ref([]);
const materiaList = ref([]);//物料表
const wareList = ref([]);//仓库
const procedureOrderList = ref([]);//工序订单
const supplierList = ref([]);
const storageAllList = ref([]);
// 供应商远端搜索
const supplierSearch = async (query: string) => {
  if (query) {
    try {
      const response = await axiosInstance.post(`/api/basedataSupplier/getPage`, {
        current: 1,
        pageSize: 20,
        name: query
      });
      supplierList.value = await response.data.records; // 假设返回的数据直接是供应商列表
    } catch (error) {
      supplierList.value = []; // 出错时清空列表
    }
  }
};
const deviceList = ref([])
const materialList = ref([]); // 物料表

const getOption = () => {
  axiosInstance.post('/api/basedataMaterial/getAll', {})
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      materialList.value = res.data;
    });
  // axiosInstance.post('/api/basedataEquipment/getAll', {})
  //   .then((res) => {
  //     if (res.code !== 200) {
  //       ElMessage.warning(res.message);
  //       return false;
  //     }
  //     deviceList.value = res.data;
  //   });

  // axiosInstance.post('/api/outsourcingPlan/querySourcingPlanProcedureOrder', {})
  //   .then(res => {
  //     workOrderList.value = res.data;
  //   });

  // // 查询所有库位
  // axiosInstance.post('/api/basedataStorage/getAll', {})
  //   .then(res => {
  //     storageAllList.value = res.data;
  //   });


  // axiosInstance.post(`/api/basedataSupplier/getPage`, {
  //   current: 1,
  //   pageSize: 20
  // }).then(res => {
  //   if (res.code == 200) {
  //     supplierList.value = res.data.records;
  //   }
  // });


  // axiosInstance.post('/api/planProcedureOrder/getAll', {})
  //   .then(res => {
  //     if (res.code !== 200) {
  //       ElMessage.error(res.message);
  //       return false;
  //     }
  //     const list = res.data;
  //     const loop = [];
  //     for (const item of list) {
  //       loop.push(
  //         {
  //           value: item.id,
  //           label: item.materialName
  //         }
  //       );
  //     }
  //     procedureOrderList.value = list;
  //   });

  axiosInstance.get('/api/basedataWarehouse/treeList', {}).then(res => {
    if (res.code !== 200) {
      ElMessage.error(res.message);
      return false;
    }
    wareList.value = res.data;
  });
};

const wareChange = (e, type) => {
  // searchParam.value.storageId = null
  // storageList.value = {}
  axiosInstance.post("/api/basedataStorage/listByPojoPage", {
    warehouseId: e,
    current: 1,
    pageSize: 999999
  })
    .then((res) => {
      if (res.code !== 200) {
        ElMessage.warning(res.message);
        return false
      }
      storageList.value = res.data.records
      searchParam.value.targetStorageId =  storageList.value.map(item => item.id)
    })
}
// 获取列表接口
const getList = () => {
  axiosInstance.post('/api/purchaseinstocklog/getPage', {
    ...searchParam.value,
    ...pageSearch.value,
    ...{
      status: 1,//1 入库 0出
      type: 1 //1 原材料  
    }
  })
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      const list = res.data.records;
      for (const item of list) {
        item.children = item.outsourcingWorkMaterialVOS;
      }
      paginatedData.value = list;
      pageTotal.value = res.data.total;
    });
};

const handleSizeChange = (e) => {
  pageSearch.value.pageSize = e;
  getList();
};

const handleCurrentChange = (e) => {
  pageSearch.value.current = e;
  getList();
};

// 重置搜索条件
const resetSearch = () => {
  searchParam.value = {};
  pageSearch.value.current = 1;
  // current.value = 1
  getList();
};

</script>
<style scoped></style>
